﻿/* 
 * This file is part of marvellous-components project.
 * 
 * The marvellous-components is free software: you can redistribute it 
 * and/or modify it under the terms of the GNU General Public License 
 * version 3 as published by the Free Software Foundation.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace marvellous_components.AwesomeTextBox
{
    /// <summary>
    /// Form which contains autocomplete list. For detailed desription see AwesomeTextBox.cs
    /// </summary>
    internal partial class AwesomeTextBoxAutocomplete : Form
    {
        private AwesomeTextBox slave;

        private Form acceptCancelForm;

        private IButtonControl formCancel;
        private IButtonControl formAccept;

        private string[] items = new string[]{};

        internal AwesomeTextBoxAutocomplete(AwesomeTextBox slave, Form acceptCancelForm)
        {
            InitializeComponent();
            this.Size = this.listBox.Size;
            this.slave = slave;
            this.acceptCancelForm = acceptCancelForm;
            this.formAccept = acceptCancelForm.AcceptButton;
            this.formCancel = acceptCancelForm.CancelButton;
        }

        internal void SetItems(string[] items)
        {
            this.items = items;
            this.listBox.Items.Clear();
            this.listBox.Items.AddRange(items);
            resize();
        }

        internal void Search(string query)
        {
            query = query.ToLower();
            if (query.Length > 0)
            {
                listBox.Items.Clear();

                string[] querySplit = query.Split(' ');

                for (int i = 0; i < items.Length; i++)
                {
                    bool foundFlag = true;
                    foreach (string queryPart in querySplit)
                    {
                        if (!(items[i].ToLower().Contains(queryPart)))
                        {
                            foundFlag = false;
                            break;
                        }
                    }
                    if (foundFlag)
                    {
                        listBox.Items.Add(items[i]);
                    }
                }
            }
            else
            {
                listBox.Items.Clear();
                listBox.Items.AddRange(items);
            }
            if (listBox.Items.Count > 0)
            {
                listBox.SelectedIndex = 0;
            }
            resize();
        }

        internal void pushSelectedToSlave()
        {
            if (listBox.SelectedIndex != -1)
            {
                slave.Text = (string)listBox.Items[listBox.SelectedIndex];
            }
            slave.hideAutocompleteWindow();
            slave.Focus();
        }

        private void resize()
        {
            Size newSize = listBox.GetPreferredSize(listBox.Size);
            Point listBoxPos = listBox.PointToScreen(new Point(0, 0));

            int maxHeight = Screen.PrimaryScreen.WorkingArea.Height - listBoxPos.Y;
            int maxWidth = Screen.PrimaryScreen.WorkingArea.Width - listBoxPos.X;

            if (newSize.Height > maxHeight)
            {
                newSize.Height = maxHeight;
            }
            if (newSize.Width > maxWidth)
            {
                newSize.Width = maxWidth;
            }

            if (listBox.Items.Count == 0)
            {
                listBox.BackColor = Color.FromArgb(255, 120, 120);
            }
            else
            {
                listBox.BackColor = Color.FromKnownColor(KnownColor.Window);
            }
            listBox.Size = newSize;
            this.Size = listBox.Size;
        }

        private void listBox_DoubleClick(object sender, EventArgs e)
        {
            pushSelectedToSlave();
        }

        private void listBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                pushSelectedToSlave();
            }
            else if (e.KeyChar == (char)Keys.Escape)
            {
                listBox.SelectedIndex = -1;
                pushSelectedToSlave();
            }
            else if (Char.IsLetterOrDigit(e.KeyChar))
            {
                slave.AppendText(e.KeyChar.ToString());
                slave.Focus();
            }
            else if (e.KeyChar == (char)Keys.Back)
            {
                slave.Text = slave.Text.Substring(0, slave.Text.Length - 1);
                slave.SelectionStart = slave.Text.Length;
                slave.Focus();
            }
            else if (((ModifierKeys & Keys.Control) == Keys.Control) && (e.KeyChar == (char)Keys.Space))
            {
                slave.this_KeyPress(sender, e);
            }
        }

        private void AwesomeTextBoxAutocomplete_VisibleChanged(object sender, EventArgs e)
        {
            if (this.Visible)
            {
                acceptCancelForm.AcceptButton = null;
                acceptCancelForm.CancelButton = null;
            }
            else
            {
                acceptCancelForm.AcceptButton = formAccept;
                acceptCancelForm.CancelButton = formCancel;
            }
        }

        private void AwesomeTextBoxAutocomplete_Shown(object sender, EventArgs e)
        {
            resize();
        }
    }
}
